package org.vectrics.company.web.struts;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;

import org.apache.log4j.Category;
import org.apache.struts.action.ActionForm;
import org.vectrics.struts.Request;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;

abstract public class RequestFormAction extends VectricsStrutsAction {
    private static Category log = Category.getInstance(RequestFormAction.class);
	public static String JOB_PARAM = "job";

	abstract protected ActionForm createForm();
	
	abstract protected String fillForm(ActionForm form, Request request,
			 HttpServletResponse response);

	
	protected String doExecute(VectricsActionData actionData)	throws JspException {
		if (actionData.getForm() == null) {
			actionData.setForm(createForm());
		}
		
		if (actionData.getForm() == null)
		    throw new JspException("Form is null exception");
				
		if ("request".equals(actionData.getMapping().getScope())) {
			log.info("Company form attached to request, attribute name = " 
					+ actionData.getMapping().getAttribute());
			actionData.getRequest().setAttribute(actionData.getMapping().getAttribute(), actionData.getForm());
		}
		else {
			actionData.getRequest().setSessionAttribute(actionData.getMapping().getAttribute(), actionData.getForm());
		}
		String mapCode = fillForm(actionData.getForm(), actionData.getRequest(), actionData.getResponse());
		saveToken(actionData.getRequest().getHttpRequest());
				 	
		return (mapCode);
	}
}
